package com.p2p.terminal.controller;

import com.p2p.common.base.BaseController;
import com.p2p.common.utils.StringUtils;
import com.p2p.common.utils.Underline2Camel;
import com.p2p.terminal.service.MacService;
import com.p2p.terminal.vo.MacCondition;
import com.p2p.terminal.vo.MacErrorCondition;
import com.p2p.terminal.vo.MacErrorResult;
import com.p2p.terminal.vo.MacResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Allen on 2017/3/19.
 */
@RestController
@RequestMapping(value = "mac")
public class MacController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MacController.class);
    @Autowired
    private MacService macService;

    /**
     * 获取分组列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "selectP2pMac", method = RequestMethod.GET)
    public Map selectP2pMac(HttpServletRequest request) {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String agentCode = request.getParameter("agentCode").equals("0") ? null : request.getParameter("agentCode");


        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String sortField = Underline2Camel.camel2Underline(request.getParameter("sort") == null ? "id" : request.getParameter("sort"), true);
        String orderField = request.getParameter("order") == null ? "desc" : request.getParameter("order");
        int startRow = (pageIndex - 1) * pageSize;

        MacCondition macCondition = new MacCondition();
        macCondition.setStartDate(startDate);
        macCondition.setEndDate(endDate);
        macCondition.setAgentCode(agentCode);
        macCondition.setStartRow(startRow);
        macCondition.setPageSize(pageSize);
        macCondition.setSortField(sortField);
        macCondition.setOrderField(orderField);

        MacResult macResult = macService.selectP2pMac(macCondition);
        Map result = new HashMap();
        result.put("rel", true);
        result.put("msg", "SUCCESS");
        result.put("list", macResult.getP2pMacList());
        result.put("count", macResult.getSize());
        return result;
    }


    /**
     * 获取分组列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "selectP2pMacError", method = RequestMethod.GET)
    public Map selectP2pMacError(HttpServletRequest request) {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String agentCode = request.getParameter("agentCode").equals("0") ? null : request.getParameter("agentCode");
        String cmac = StringUtils.isEmpty(request.getParameter("cmac")) ? null : request.getParameter("cmac");


        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String sortField = Underline2Camel.camel2Underline(request.getParameter("sort") == null ? "id" : request.getParameter("sort"), true);
        String orderField = request.getParameter("order") == null ? "desc" : request.getParameter("order");
        int startRow = (pageIndex - 1) * pageSize;

        MacErrorCondition macErrorCondition = new MacErrorCondition();
        macErrorCondition.setStartDate(startDate);
        macErrorCondition.setEndDate(endDate);
        macErrorCondition.setAgentCode(agentCode);
        macErrorCondition.setCmac(cmac);
        macErrorCondition.setStartRow(startRow);
        macErrorCondition.setPageSize(pageSize);
        macErrorCondition.setSortField(sortField);
        macErrorCondition.setOrderField(orderField);

        MacErrorResult macErrorResult = macService.selectP2pMacError(macErrorCondition);
        Map result = new HashMap();
        result.put("rel", true);
        result.put("msg", "SUCCESS");
        result.put("list", macErrorResult.getP2pMacErrorList());
        result.put("count", macErrorResult.getSize());
        return result;
    }
}
